package com.tutu.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.tutu.model.bean.UserInfo;
import com.tutu.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 图图 2020/10/11
 * 联系人的操作类
 */
public class ContactTableDao {
    private DBHelper mHelper;

    public ContactTableDao(DBHelper mHelper) {
        this.mHelper = mHelper;
    }

    /**
     * 获取所有联系人
     * @return
     */
    public List<UserInfo> getContacts(){
        List<UserInfo>  userList = new ArrayList<>();
        SQLiteDatabase database = mHelper.getReadableDatabase();
        String sql = "select * from " + ContactTable.TAB_NAME + " where " + ContactTable.COL_IS_CONTACT + "=1";
        Cursor cursor = database.rawQuery(sql, null);
        while (cursor.moveToNext()){
            UserInfo userInfo = new UserInfo();
            // 封装数据
            userInfo.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
            userInfo.setUsername(cursor.getString(cursor.getColumnIndex(ContactTable.COL_USERNAME)));
            userInfo.setUserpwd(cursor.getString(cursor.getColumnIndex(ContactTable.COL_USERPWD)));
            userInfo.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
            userInfo.setPhoto(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHOTO)));
            userInfo.setPhone(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHONE)));
            userInfo.setEmail(cursor.getString(cursor.getColumnIndex(ContactTable.COL_EMAIL)));
            userInfo.setGender(cursor.getInt(cursor.getColumnIndex(ContactTable.COL_GENDER)));
            userInfo.setRealname(cursor.getString(cursor.getColumnIndex(ContactTable.COL_REALNAME)));
            userList.add(userInfo);
        }
        cursor.close();
        return  userList;
    }

    /**
     * 通过环信id获取单个人的信息
     * @param hxid
     * @return
     */
    public  UserInfo getContactByHx(String hxid){
        if(TextUtils.isEmpty(hxid))  return  null;

        // 1.获取数据库对象
        SQLiteDatabase db = mHelper.getReadableDatabase();

        // 2.执行查询语句
        String sql = "select * from " + ContactTable.TAB_NAME + " where " + ContactTable.COL_HXID + "=?";

        Cursor cursor = db.rawQuery(sql, new String[]{hxid});
        UserInfo userInfo = null;
        if(cursor.moveToNext()){
            userInfo = new UserInfo();
            // 封装数据
            userInfo.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
            userInfo.setUsername(cursor.getString(cursor.getColumnIndex(ContactTable.COL_USERNAME)));
            userInfo.setUserpwd(cursor.getString(cursor.getColumnIndex(ContactTable.COL_USERPWD)));
            userInfo.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
            userInfo.setPhoto(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHOTO)));
            userInfo.setPhone(cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHONE)));
            userInfo.setEmail(cursor.getString(cursor.getColumnIndex(ContactTable.COL_EMAIL)));
            userInfo.setGender(cursor.getInt(cursor.getColumnIndex(ContactTable.COL_GENDER)));
            userInfo.setRealname(cursor.getString(cursor.getColumnIndex(ContactTable.COL_REALNAME)));
        }

        // 3.关闭资源
        cursor.close();

        // 4.返回数据
        return  userInfo;
    }

    /**
     * 通过环信id获取用户联系人信息
     * @param hxids
     * @return
     */
    public List<UserInfo> getContactsByHx(List<String> hxids){
        if(hxids == null || hxids.size() <= 0) return null;

        List<UserInfo>  userList = new ArrayList<>();
        for(String hxid : hxids){
           UserInfo userInfo = getContactByHx(hxid);
           if(userInfo != null) userList.add(userInfo);
        }
        return  userList;
    }

    /**
     * 保存单个联系人
     * @param user
     * @param isMyContact
     */
    public void saveContact(UserInfo user,boolean isMyContact){
        if(user == null) return;
        SQLiteDatabase database = mHelper.getReadableDatabase();

        ContentValues values = new ContentValues();
        values.put(ContactTable.COL_HXID,user.getHxid());
        values.put(ContactTable.COL_USERNAME,user.getUsername());
        values.put(ContactTable.COL_USERPWD,user.getUserpwd());
        values.put(ContactTable.COL_NICK,user.getNick());
        values.put(ContactTable.COL_PHOTO,user.getPhoto());
        values.put(ContactTable.COL_PHONE,user.getPhone());
        values.put(ContactTable.COL_EMAIL,user.getEmail());
        values.put(ContactTable.COL_GENDER,user.getGender());
        values.put(ContactTable.COL_REALNAME,user.getRealname());
        values.put(ContactTable.COL_IS_CONTACT,isMyContact ? 1 : 0);

        database.replace(ContactTable.TAB_NAME,null,values);
    }

    /**
     * 保存联系人信息
     * @param
     * @param isMycontact
     */
    public  void saveContacts(List<UserInfo> contacts,boolean isMycontact){
        if(contacts == null || contacts.size() <= 0) return;
        for(UserInfo user : contacts){
            saveContact(user,isMycontact);
        }
    }

    /**
     * 删除联系人信息
     * @param hxid
     */
    public  void deleteContactByHxId(String hxid){
        if(TextUtils.isEmpty(hxid)) return;
        SQLiteDatabase database = mHelper.getReadableDatabase();
        database.delete(ContactTable.TAB_NAME,ContactTable.COL_HXID + "=?",new String[]{hxid});

    }


}
